<template>
  <view class="nav-bar">
    <view class="nav-item" @tap="navTo('home')" :class="{ active: active === 0 }">
      <image class="nav-icon" :src="active === 0 ? '/static/images/1-002.png' : '/static/images/1-001.png'" mode="aspectFit"></image>
      <text class="nav-text" :class="{ active: active === 0 }">首页</text>
    </view>
    
    <view class="nav-item" @tap="navTo('order')" :class="{ active: active === 1 }">
      <image class="nav-icon" :src="active === 1 ? '/static/images/3-002.png' : '/static/images/3-001.png'" mode="aspectFit"></image>
      <text class="nav-text" :class="{ active: active === 1 }">订单</text>
    </view>
    
    <view class="nav-item" @tap="navTo('viewed')" :class="{ active: active === 2 }">
      <image class="nav-icon" :src="active === 2 ? '/static/images/2-002.png' : '/static/images/2-001.png'" mode="aspectFit"></image>
      <text class="nav-text" :class="{ active: active === 2 }">看过</text>
    </view>
    
    <!-- <view class="nav-item" @tap="navTo('mine')" :class="{ active: active === 3 }">
      <view class="iconfont icon-wode" :class="{ active: active === 3 }"></view>
      <text class="nav-text" :class="{ active: active === 3 }">我的</text>
    </view> -->
  </view>
</template>

<script>
export default {
  name: 'CustomTabBar',
  props: {
    active: {
      type: Number,
      default: 0
    }
  },
  methods: {
    navTo(page) {
      console.log('导航组件点击：', page);
      
      let pageIndex = 0;
      
      if (page === 'home') {
        pageIndex = 0;
        // 民宿系统使用返回功能而不是跳转到首页
        const pages = getCurrentPages();
        if (pages.length > 1) {
          uni.navigateBack({
            animationType: 'none',
            animationDuration: 0
          });
        } else {
          // 如果没有上一页，可以关闭当前页面或保持当前页面
          console.log('已在根页面，无需返回');
        }
      } else if (page === 'order') {
        pageIndex = 1;
        // 跳转到房间订单页面
        uni.navigateTo({
          url: '/pages/users/roomorder/roomorder',
          animationType: 'none',
          animationDuration: 0
        });
      } else if (page === 'viewed') {
        pageIndex = 2;
        // 跳转到房源浏览记录页面
        uni.navigateTo({
          url: '/pages/house/history/index',
          animationType: 'none',
          animationDuration: 0
        });
      } 
      // else if (page === 'mine') {
      //   pageIndex = 3;
      //   // 跳转到个人中心页面
      //   uni.switchTab({
      //     url: '/pages/user/index',
      //     animationType: 'none',
      //     animationDuration: 0,
      //     fail: () => {
      //       uni.navigateTo({
      //         url: '/pages/user/index',
      //         animationType: 'none',
      //         animationDuration: 0
      //       });
      //     }
      //   });
      // }
      
      this.$emit('change', pageIndex);
    }
  }
}
</script>

<style scoped>
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-around;
  /* 兼容iOS 11.2以下版本 */
  height: calc(120rpx + constant(safe-area-inset-bottom) + 10rpx);
  padding: 20rpx 20rpx calc(constant(safe-area-inset-bottom) + 10rpx) 20rpx;
  /* iOS 11.2+和其他浏览器 */
  height: calc(120rpx + env(safe-area-inset-bottom) + 10rpx);
  padding: 20rpx 20rpx calc(env(safe-area-inset-bottom) + 10rpx) 20rpx;
  background-color: #ffffff;
  border-radius: 0;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.08);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  border-top: 1rpx solid #f0f0f0;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 120rpx;
  flex: 1;
  padding: 10rpx 0;
  transition: all 0.3s ease;
}

.nav-item .nav-icon {
  width: 44rpx;
  height: 44rpx;
  margin-bottom: 4rpx;
  transition: all 0.3s ease;
}

.nav-item .nav-icon.active {
  transform: scale(1.1);
}

.nav-text {
  font-size: 20rpx;
  color: #999999;
  transition: all 0.3s ease;
  line-height: 1.2;
}

.nav-text.active {
  color: #ff6b6b;
  font-weight: 600;
}

.nav-item.active {
  transform: translateY(-2rpx);
}

/* 返回图标样式 */
.nav-item .iconfont {
  font-size: 44rpx;
  color: #999999;
  transition: all 0.3s ease;
  margin-bottom: 4rpx;
}

.nav-item .iconfont.active {
  color: #ff6b6b;
  transform: scale(1.1);
}

/* 图标样式已改为图片，无需字体映射 */
</style> 